package service.Impl;

import com.alibaba.fastjson.JSONObject;
import entity.TeacherTable;
import mapper.TeacherTableDao;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import service.TeacherTableService;
import utils.ExcelImportUtils;
import utils.PageObject;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * (TeacherTable)表服务实现类
 *
 * @author makejava
 * @since 2022-01-17 14:33:21
 */
@Service("teacherTableService")
public class TeacherTableServiceImpl implements TeacherTableService {
    @Resource
    private TeacherTableDao teacherTableDao;

    /**
     * 通过ID查询单条数据
     *
     * @param pkId 主键
     * @return 实例对象
     */
    @Override
    public TeacherTable queryById(Integer pkId) {
        return this.teacherTableDao.queryById(pkId);
    }


    /**
     * 查询多条数据
     *
     * @param pageobject
     * @return 对象列表
     */
    @Override
    public PageObject queryAllByLimit(JSONObject pageobject) {
        PageObject pageObject = new PageObject();
        Integer offset = pageobject.getInteger("page");
        Integer limit = pageobject.getInteger("limit");
        //计算开始位置
        Integer startindex = (offset - 1) * limit;
        List<TeacherTable> teacherTables = this.teacherTableDao.queryAllByLimit(startindex, limit);
        System.out.println("=========" + teacherTables.toString());
        int selectcount = this.teacherTableDao.selectcount();
        pageObject.setPageCount(offset);//第几页
        pageObject.setPageSize(limit);//每页几条
        pageObject.setRowCount(selectcount);//总条数
        pageObject.setPageCount((selectcount - 1) / offset + 1);//总页数
        pageObject.setRecords(Collections.singletonList(teacherTables));//当前需展示数据
        return pageObject;
    }

    /**
     * 新增数据
     *
     * @param insertobj 实例对象
     * @return 实例对象
     */
    @Override
    public JSONObject insert(JSONObject insertobj) throws ParseException {
        JSONObject result = new JSONObject();
        TeacherTable teacherTable = new TeacherTable();
        if (insertobj.getString("teacherid")!=null){
            teacherTable.setTeacherid(insertobj.getString("teacherid"));
        }
        if (insertobj.getString("mainteacher")!=null){
            teacherTable.setMainteacher(insertobj.getString("mainteacher"));
        }
        if (insertobj.getString("viceteacher")!=null){
            teacherTable.setViceteacher(insertobj.getString("viceteacher"));
        }
        if (insertobj.getString("xunkaoteacher")!=null){
            teacherTable.setXunkaoteacher(insertobj.getString("xunkaoteacher"));
        }
        if (insertobj.getString("iscancelinvigilate")!=null){
            teacherTable.setIscancelinvigilate(insertobj.getString("iscancelinvigilate"));
        }
        if (insertobj.getString("maxinvigilatetimes")!=null){
            teacherTable.setMaxinvigilatetimes(insertobj.getString("maxinvigilatetimes"));
        }

        teacherTable.setPkId(insertobj.getInteger("pkId"));


        if (teacherTable.getPkId() == null || teacherTable.getPkId().equals(0)) {

            if(insertobj.getString("kstime")!=null){
                Calendar calendar = Calendar.getInstance();


                SimpleDateFormat sf =new SimpleDateFormat("yyyy/MM/dd");//时间格式话

                Date parse = sf.parse(insertobj.getString("kstime"));
                teacherTable.setKstime(parse);
            }

            int insert = this.teacherTableDao.insert(teacherTable);
            if (insert == 1) {
                result.put("code", "0");
                result.put("msg", "数据插入成功!");
            } else {
                result.put("code", "-1");
                result.put("msg", "数据插入失败,请核对!");
            }
        } else {
            if(insertobj.getDate("kstime")!=null){
                teacherTable.setKstime(insertobj.getDate("kstime"));
            }

            int update =   this.teacherTableDao.update(teacherTable);
            if (update == 1) {
                result.put("code", "0");
                result.put("msg", "数据修改成功!");
            } else {
                result.put("code", "-1");
                result.put("msg", "数据修改失败,请核对!");
            }

        }
        return result;
    }

    /**
     * 修改数据
     *
     * @param teacherTable 实例对象
     * @return 实例对象
     */
    @Override
    public TeacherTable update(TeacherTable teacherTable) {
        this.teacherTableDao.update(teacherTable);
        return this.queryById(teacherTable.getPkId());
    }

    /**
     * 通过主键删除数据
     *
     * @param pkId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer pkId) {
        return this.teacherTableDao.deleteById(pkId) > 0;
    }

    @Override
    public JSONObject importtoExcel(MultipartFile file) throws Exception {
        List<TeacherTable> list = new ArrayList<>();
        JSONObject result = new JSONObject();
        Workbook workbook = null;
        String fileName = file.getOriginalFilename();
        if (fileName.endsWith("xls")) {
            workbook = new HSSFWorkbook(file.getInputStream());
        } else if (fileName.endsWith("xlsx")) {
            workbook = new XSSFWorkbook(file.getInputStream());
        } else {
            throw new Exception("传输文件不是Excel文件!");
        }
        Sheet sheet = workbook.getSheet("Sheet1");
        int rows = sheet.getLastRowNum();
        if (rows == 0) {
            throw new Exception("行数不能为0");
        }
        int s = 0;
        for (int i = 1; i < rows + 1; i++) {
            //读取左上端单元格
            Row row = sheet.getRow(i);
            //行不为空
            if (row != null) {
                //读取cell
                TeacherTable teacherTable = new TeacherTable();
                //教师id
                String teacherid = ExcelImportUtils.getCellValue(row.getCell(0));
                teacherTable.setTeacherid(teacherid);
                //主监考老师
                String mainteacher = ExcelImportUtils.getCellValue(row.getCell(1));
                teacherTable.setMainteacher(mainteacher);
                //副监考老师
                String viceteacher = ExcelImportUtils.getCellValue(row.getCell(2));
                teacherTable.setViceteacher(viceteacher);
                //巡考老师
                String xunkaoteacher = ExcelImportUtils.getCellValue(row.getCell(3));
                teacherTable.setXunkaoteacher(xunkaoteacher);
                //是否免监考
                String iscancelInvigilate = ExcelImportUtils.getCellValue(row.getCell(4));
                teacherTable.setIscancelinvigilate(iscancelInvigilate);
                //最对监考次数
                String maxInvigilatetimes = ExcelImportUtils.getCellValue(row.getCell(5));
                teacherTable.setMaxinvigilatetimes(maxInvigilatetimes);
                int insert = teacherTableDao.insert(teacherTable);
                s++;
                list.add(teacherTable);//把实数据放入集合里
            }
        }
        if (s == rows) {
            result.put("code", "0");
            result.put("msg", "数据导入成功!");
        } else {
            result.put("code", "-1");
            result.put("msg", "数据导入失败!");
        }
        return result;
    }

    public String isEmpty(String str) {
        String resultstr = "";
        if (!StringUtils.isEmpty(str)) {
            resultstr = str;
        }
        return resultstr;
    }
}
